﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThreeByte.Media;
using System.Xml.Linq;
using ThreeByte.ActiveDeck.Config;

namespace ThreeByte.ActiveDeck.AddIn.Content.Controls {
    public class DesignerWebView : DesignerControl {

        private string _url;
        public string URL {
            get {
                return _url;
            }
            set {
                _url = value;
                NotifyPropertyChanged("URL");
            }
        }

        public DesignerWebView(string url, PixelSpace pos)
            : base(pos) {
            URL = url;
        }

        public DesignerWebView(XElement node, Dictionary<Guid, GraphicResource> resources) {
            try {
                int top = int.Parse(node.Attribute("Top").Value);
                int left = int.Parse(node.Attribute("Left").Value);
                int width = int.Parse(node.Attribute("Width").Value);
                int height = int.Parse(node.Attribute("Height").Value);

                URL = node.Attribute("Url").Value.ToString();

                DefaultPixelSpace = new PixelSpace() { W = 768, H = 1024, X = 0, Y = 0 };
                CurrentPixelSpace = new PixelSpace() { W = width, H = height, X = left, Y = top };

            } catch(Exception ex) {
                throw ex;
            }
        }

        public override System.Xml.Linq.XElement ToXml() {
            XElement node = base.ToXml();

            node.Add(PixelSpaceAsAttributes(CurrentPixelSpace));

            if(URL != null) {
                node.Add(new XAttribute("Url", URL));
            }

            return node;
        }

        public override XElement GetPresentationXml(PixelTranslator translation = null) {
            XElement node = new XElement(ns + "WebView");
            node.Add(new XAttribute("Url", URL));

            PixelSpace pos = CurrentPixelSpace;
            if(translation != null) {
                pos = translation.Translate(pos);
            }
            node.Add(PixelSpaceAsAttributes(pos));

            return node;
        }


    }
}
